package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcTransferEntity;
import com.dhcc.cdc.busi.service.ICdcTransferService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 转科记录
 * 
 * @ClassName: CdcTransferController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:06
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdctransfer")
public class CdcTransferController extends BaseController {

	private String PREFIX = "/backend/cdc/cdctransfer/";

	@Autowired
	private ICdcTransferService cdcTransferService;
	

    /**
     * 跳转到转科记录列表首页
     */
    @RequestMapping("")
    @Permission("cdctransfer:list")
    public String index() {
        return PREFIX + "cdctransfer.html";
    }
	
    /**
     * 跳转到新增转科记录
     */
    @RequestMapping("/to_add")
    @Permission("cdctransfer:add")
    public String toAdd() {
        return PREFIX + "cdctransfer_add.html";
    }
	
	/**
     * 跳转到修改转科记录
     */
    @RequestMapping("/to_update/{cdcTransferId}")
    @Permission("cdctransfer:update")
    public String toUpdate(@PathVariable Long cdcTransferId, Model model) {
        CdcTransferEntity cdcTransfer = this.cdcTransferService.selectById(cdcTransferId);
        model.addAttribute("cdcTransfer",cdcTransfer);
        return PREFIX + "cdctransfer_edit.html";
    }

    /**
     * 获取转科记录列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdctransfer:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcTransferEntity> page = new PageFactory<CdcTransferEntity>().defaultPage();
        List<CdcTransferEntity> result = this.cdcTransferService.page(page,params);
        page.setRecords((List<CdcTransferEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增转科记录
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdctransfer:add")
    @BussinessLog(value = "新增转科记录")
    public Object add(CdcTransferEntity cdcTransfer) {
        if (CommonUtil.isOneEmpty(cdcTransfer)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcTransfer.insert();
        return R.ok();
    }

    /**
     * 删除转科记录
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdctransfer:delete")
    @BussinessLog(value = "删除转科记录")
    public Object delete(@RequestParam Long cdcTransferId) {
	
        this.cdcTransferService.deleteById(cdcTransferId);

        return R.ok();
    }

    /**
     * 修改转科记录
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdctransfer:update")
    @BussinessLog(value = "修改转科记录")
    public Object update(CdcTransferEntity cdcTransfer) {
        if (CommonUtil.isOneEmpty(cdcTransfer)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcTransferEntity old = this.cdcTransferService.selectById(cdcTransfer.getId());

        old.setXcode(cdcTransfer.getXcode());
        old.setEpisodeId(cdcTransfer.getEpisodeId());
        old.setBusTranTypeId(cdcTransfer.getBusTranTypeId());
        old.setTranLocId(cdcTransfer.getTranLocId());
        old.setTranBedId(cdcTransfer.getTranBedId());
        old.setFromLocId(cdcTransfer.getFromLocId());
        old.setTranDate(cdcTransfer.getTranDate());
        old.setFromDate(cdcTransfer.getFromDate());
        old.setInLocDays(cdcTransfer.getInLocDays());
        old.setDataId(cdcTransfer.getDataId());
        old.setIsActive(cdcTransfer.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
